package org.dftproject.genesis.ui.editors;

import org.dftproject.genesis.ui.ColorUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.part.EditorPart;

public abstract class EditorPartEx extends EditorPart {

	private Color backgroundColor;
	private Composite container;
	
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout());
		
		Display display = parent.getDisplay();
		backgroundColor = new Color(display, ColorUtils.blend(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB(), display.getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB()));
		
		container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginWidth = layout.marginHeight = layout.marginTop = layout.marginRight = layout.marginBottom = layout.marginLeft = layout.verticalSpacing = 0;
		container.setLayout(layout);
		container.setBackground(backgroundColor);
		container.setBackgroundMode(SWT.INHERIT_DEFAULT);
		
		Composite body = new Composite(container, SWT.NONE); 
		body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		body.setLayout(new FillLayout());
		
		createControls(body);
	}
	
	abstract public void createControls(Composite parent);
	
	@Override
	public void dispose() {
		backgroundColor.dispose();
		super.dispose();
	}
	
	public void setBackground(Color color) {
		container.setBackground(color == null ? backgroundColor : color);
	}
	
}
